package com.bd.volkswagen.config;

//import com.bd.scm.interceptor.SecurityInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

///**
// * Spring Boot 2.0 解决跨域问题
// *
// */
//@Configuration
//public class WebMvcConfig implements WebMvcConfigurer {
//
//	@Bean
//	public CorsFilter corsFilter() {
//		final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
//		final CorsConfiguration corsConfiguration = new CorsConfiguration();
//		/* 是否允许请求带有验证信息 */
//		corsConfiguration.setAllowCredentials(true);
//		/* 允许访问的客户端域名 */
//		corsConfiguration.addAllowedOrigin("*");
//		/* 允许服务端访问的客户端请求头 */
//		corsConfiguration.addAllowedHeader("*");
//		/* 允许访问的方法名,GET POST等 */
//		corsConfiguration.addAllowedMethod("*");
//		urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
//		return new CorsFilter(urlBasedCorsConfigurationSource);
//	}
//}

/**
 * @author yufc
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    /**
     * 定义排除拦截路径
     */
    public String[] EXCLUDE_PATH_PATTERN = {
            "/druid/**", "/api/login/**", "/api/test/**",
            "/doc.html", "/swagger-resources/**", "/v2/api-docs/**", "/v2/api-docs-ext/**", "/webjars/**",
            "/filedownload/toDownload",
            "/oauth2/**",
            "/wXLogin/**",
            "/api/visitor/**",
            "/uploadFile/upload",
            "/approval/**",

            "/api/sync/**",
            "/wXLogin/**",
            "/api/user/**",
            "/api/parkingLot/**", //舍弗勒停车场远程管理
            "/api/sys/log/**", //舍弗勒日志管理
            "/api/stDevice/**" //设备
    };

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/effective/**").addResourceLocations("file:D:\\effective\\");
        super.addResourceHandlers(registry);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //设置允许跨域的路径
        registry.addMapping("/**")
                //设置允许跨域请求的域名
                .allowedOrigins("*")
                //是否允许证书 不再默认开启
                .allowCredentials(true)
                //设置允许的方法
                .allowedMethods("*")
                //跨域允许时间
                .maxAge(3600);
    }

    /**
     * 注册自定义拦截器，添加拦截路径和排除拦截路径
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(new SecurityInterceptor()).addPathPatterns("/**").excludePathPatterns(EXCLUDE_PATH_PATTERN);

    }
}
